﻿using System;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES1002
{
    [Plug(ID = "cMES1002",
          Name = "工站资料",
          Description = "工站资料维护",
          Parent = "0,基础数据")]
    public partial class cMES1002 : StdView
    {
        public cMES1002()
        {
            InitializeComponent();         
        }
        private void cMES1002_Load(object sender, EventArgs e)
        {
            //initial current group
            this.GetGroupNameList();
            //get system section name list
            this.GetSectionNameList();
            //get group config info list
            this.GetGroupCfgList();
        }
     
        private void GetGroupNameList()
        {
            string _strSection = this.combSectionName.Text.Trim().ToUpper();
            var result = Services.ServiceMethod.GetGroupNameList(_strSection);
            if (result.Status)
            {
                this.combGroupName.Items.Clear();
                foreach(var tmpGroup in result.Data)
                {
                    this.combGroupName.Items.Add(tmpGroup.group_name);
                }
            }
        }

        private void GetSectionNameList()
        {
            var result = Services.ServiceMethod.GetSectionNameList();
            if (result.Status)
            {
                this.combSectionName.Items.Clear();
                foreach(var tmpSection in result.Data)
                {
                    this.combSectionName.Items.Add(tmpSection.section_name);
                }
            }
        }

        private void GetGroupCfgList()
        {
            string _strGroup = this.combGroupName.Text.Trim().ToUpper();
            string _strSection = this.combSectionName.Text.Trim().ToUpper();

            var result = Services.ServiceMethod.GetGroupCfgInfoList(_strGroup,_strSection);
            if (!result.Status)
            {
                this.ShowMessage(result.Message, MsgType.NG);
                return;
            }
            if (result.Data.Count > 0)
            {
                this.datalist.DataSource = result.Data;
            }
            else
            {
                this.datalist.Rows.Clear();
                this.ShowMessage("系统没有站别配置数据。", MsgType.NG);
            }
            this.datalist.ClearSelection();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.GetGroupCfgList();
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            frmGroupCfgEdit edit = default(frmGroupCfgEdit);
            string _strCMDTag = ((ToolStripButton)sender).Tag.ToString();
            if (_strCMDTag.Equals("ADD"))
            {
                edit = new frmGroupCfgEdit(_strCMDTag);

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据添加成功！", MsgType.OK);

                    this.GetGroupCfgList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据添加失败！", MsgType.NG);
                }
            }

            if (_strCMDTag.Equals("EDIT"))
            {
                if (datalist.SelectedRows.Count < 1)
                {
                    ShowMessage("请选择一个信息进行编辑.", MsgType.NG);
                    return;
                }
                edit = new frmGroupCfgEdit(_strCMDTag, new object[] {
                        datalist.SelectedRows[0].Cells[0].Value,
                        datalist.SelectedRows[0].Cells[1].Value,
                        datalist.SelectedRows[0].Cells[2].Value,
                        datalist.SelectedRows[0].Cells[3].Value,
                        datalist.SelectedRows[0].Cells[4].Value,
                        datalist.SelectedRows[0].Cells[5].Value,
                        datalist.SelectedRows[0].Cells[6].Value,
                        datalist.SelectedRows[0].Cells[7].Value,
                        datalist.SelectedRows[0].Cells[8].Value,
                        datalist.SelectedRows[0].Cells[9].Value
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);

                    this.GetGroupCfgList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }

            if (_strCMDTag.Equals("DELETE"))
            {
                if (datalist.SelectedRows.Count < 1)
                {
                    ShowMessage("请选择一个需要删除的信息.", MsgType.NG);
                    return;
                }

                DialogResult dialog = MessageBox.Show($"确定要删除{datalist.SelectedRows[0].Cells[1].Value}吗？", "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

                if (dialog == DialogResult.Yes)
                {
                    var result = Services.ServiceMethod.DeleteGroupInfo(datalist.SelectedRows[0].Cells[0].Value.ToString());

                    if (result.Status)
                    {
                        this.ShowMessage($"数据{datalist.SelectedRows[0].Cells[1].Value}删除成功！", MsgType.OK);

                        this.GetGroupCfgList();
                    }
                }
            }
        }

        private void datalist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                frmGroupCfgEdit edit = new frmGroupCfgEdit("EDIT", new object[]
                {
                        datalist.SelectedRows[0].Cells[0].Value,
                        datalist.SelectedRows[0].Cells[1].Value,
                        datalist.SelectedRows[0].Cells[2].Value,
                        datalist.SelectedRows[0].Cells[3].Value,
                        datalist.SelectedRows[0].Cells[4].Value,
                        datalist.SelectedRows[0].Cells[5].Value,
                        datalist.SelectedRows[0].Cells[6].Value,
                        datalist.SelectedRows[0].Cells[7].Value,
                        datalist.SelectedRows[0].Cells[8].Value,
                        datalist.SelectedRows[0].Cells[9].Value
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);
                    //刷新Group清单
                    this.GetGroupCfgList();
                }
                else if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }
        }
    }
}
